package cn.david.pet.controller;

import cn.david.basic.util.AjaxResult;
import cn.david.basic.util.LoginContext;
import cn.david.basic.util.PageList;
import cn.david.pet.domain.Pet;
import cn.david.pet.domain.SearchMasterMsg;
import cn.david.pet.query.SearchMasterMsgQuery;
import cn.david.pet.service.ISearchMasterMsgService;
import cn.david.user.domian.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.getAll();
    }
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            seachMasterMsgService.publish(searchMasterMsg,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setResultObj("发布失败"+e.getMessage());
        }

    }
    //查询待处理消息  平台管理员和 店铺人员（管理员和员工）
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            return seachMasterMsgService.pending(query,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //查询已处理消息  平台管理员和 店铺人员（管理员和员工）
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){

            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            return seachMasterMsgService.processed(query, loginInfo.getId());
        }
        //前端用户查询自己的消息
    @PostMapping("/list")
    public PageList<SearchMasterMsg> myMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){

        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return seachMasterMsgService.myMsg(query, loginInfo.getId());
    }
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet){
        try {
            seachMasterMsgService.handle(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("处理失败！"+e.getMessage());
        }
    }




}
